package com.qf.homeworkssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.homeworkssm.dao.ApiDao;
import com.qf.homeworkssm.entity.ApiMapping;
import com.qf.homeworkssm.service.ApiService;
import com.qf.homeworkssm.utils.Contants;
import com.qf.homeworkssm.vo.ResultVo;
import com.qf.homeworkssm.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ApiServiceImpl implements ApiService {


    @Autowired
    private ApiDao apiDao;


    @Override
    public TableVo<ApiMapping> getApiByTable(Integer page, Integer limit, ApiMapping apiMapping) {


        //分页插件
        PageHelper.startPage(page,limit);
        List<ApiMapping> data = apiDao.getApiById(apiMapping);
        PageInfo<ApiMapping> pageInfo = new PageInfo<>(data);
        long total = pageInfo.getTotal();
        return TableVo.ok((int)total,data);
    }

    @Override
    public ResultVo inserApi(ApiMapping apiMapping) {

        //调用dao层添加元素
        Integer integer = apiDao.inserApi(apiMapping);

        if (integer>0){
            return new ResultVo(Contants.SUCCESS_CODE,"成功");
        }else {
            return new ResultVo(Contants.FAIL_CODE,"添加失败");
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo del(Integer[] ids) {

        Integer user = apiDao.del(ids);

        if (user>0){
            return new ResultVo(Contants.SUCCESS_CODE,"删除成功");
    }else {
            return new ResultVo(Contants.FAIL_CODE,"删除成功");
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo update(ApiMapping apiMapping) {

        Integer integer =apiDao.update(apiMapping);

        if (integer>0){
            return new ResultVo(Contants.SUCCESS_CODE,"修改成功");
        }else {
            return new ResultVo(Contants.FAIL_CODE,"修改成功");
        }
    }


}
